﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace HealthSystem.sysAdmin
{
    public partial class WebForm1 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtskey.Value = Request["skey"].ToString();
                txtstype.Value = Request["stype"].ToString();
            }
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            TMap map = new TMap();
            if (Request["x"] != null && Request["y"] != null)
            {
                try
                {
                    map.skey = Request["skey"];
                    map.x = Convert.ToDouble(Request["x"]);
                    map.y = Convert.ToDouble(Request["y"]);
                    map.stype = Request["stype"];

                    int n = Add(map);
                    if (n == 1)
                        ExtAspNet.Alert.Show("新增地图成功");
                    else
                        ExtAspNet.Alert.Show("新增失败");
                }
                catch
                {
                    ExtAspNet.Alert.Show("新增失败");
                }
            }
        }
        public DataTable GetMap(string skey)
        {
            string sql = "select * from Tmap where skey=@skey";
            SqlConnection conn = new SqlConnection();
            SqlCommand comm = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(comm);
            sda.Fill(ds);
            return ds.Tables[0];
        }
        public int Add(TMap map)
        {
            DataTable dt = GetMap(map.skey);
            if (dt.Rows.Count > 0)
            {
                int n = Update(map);
                return n;
            }
            else
            {
                string sql = "insert into Tmap values(@skey,@x,@y,@stype)";
                SqlConnection conn = new SqlConnection();
                SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@skey",map.skey),
                new SqlParameter("@x",map.x),
                new SqlParameter("@y",map.y),
                new SqlParameter("@stype",map.stype)
            };
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.Parameters.AddRange(sp);
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                int n = comm.ExecuteNonQuery();
                return n;
            }
        }
        public int Update(TMap map)
        {
            string sql = "update Tmap set(x=@x,y=@y) where skey=@skey";
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@x",map.x),
                new SqlParameter("@y",map.y),
                new SqlParameter("@skey",map.skey)
            };
            SqlConnection conn = new SqlConnection();
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.Parameters.AddRange(sp);
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            int n = comm.ExecuteNonQuery();
            return n;
        }
    }
    public class TMap
    {
        public string skey { get; set; }
        public double x { get; set; }
        public double y { get; set; }
        public string stype { get; set; }

    }
}